package cn.easy.rest.controller;

import cn.easy.api.TriggerService;
import cn.easy.common.pojo.EasyResult;
import cn.easy.common.pojo.TriggerCondtion;
import cn.easy.pojo.Easytrigger;
import cn.easy.pojo.User;
import cn.easy.rest.dao.JedisClient;
import cn.easy.rest.util.UserKeyUtil;
import cn.easy.vo.TriggerInfoVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/trigger")
@Controller
public class TriggerController {
    @Reference
    private TriggerService triggerService;
    @Autowired
    private JedisClient jedisClient;


    /**
     * 新增一条触发器 需要身份验证
     *
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertTrigger", method = RequestMethod.POST)
    public EasyResult insertTrigger(@RequestBody Map map, HttpServletRequest request) {

        String userKey;
        Easytrigger trigger = new Easytrigger();
        /*--------------必要的参数判断------------*/
        //用户key
        userKey = UserKeyUtil.getUserInfoFromToken(request, jedisClient);
        //比较符
        if (map.containsKey("triggerCondition")) {

            String condition = map.get("triggerCondition").toString();
            //将比较符转换成数字
            int number = conditionToNumner(condition);
            if (number == -1) return EasyResult.fail("未知的运算符");
            else trigger.setTriggerCondition(number);
        } else return EasyResult.fail("必须提供比较符");
        //临界值数据
        if (map.containsKey("triggerNumber")) {
            trigger.setTriggerNumber(map.get("triggerNumber").toString());
        } else return EasyResult.fail("没有提供临界值");
        //使用的动作编号
        if (map.containsKey("actionId")) trigger.setActionId(Integer.parseInt(map.get("actionId").toString()));
        else return EasyResult.fail("需要提供使用的动作编号");
        //所属的传感器编号
        if (map.containsKey("sensorId")) trigger.setSensorId(Integer.parseInt(map.get("sensorId").toString()));
        else return EasyResult.fail("需要提供触发器所属的传感器编号");
        //获取触发器名字
        if (map.containsKey("triggerName")) {
            trigger.setTriggerName(map.get("triggerName").toString());
        } else {
            return EasyResult.fail("缺少触发器名");
        }

        /*--------------执行新增操作------------*/
        try {
            triggerService.insertTrigger(trigger, userKey);
            return EasyResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return EasyResult.fail(e.getMessage());
        }
    }

    /**
     * 删除一条触发器 需要身份验证
     *
     * @param triggerId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteTrigger/{triggerId}", method = RequestMethod.GET)
    public EasyResult deleteTrigger(@PathVariable int triggerId,HttpServletRequest request) {
        String userKey = UserKeyUtil.getUserInfoFromToken(request,jedisClient);
        /*--------------执行删除操作------------*/
        try {
            triggerService.deleteTrigger(triggerId, userKey);
            return EasyResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return EasyResult.fail(e.getMessage());
        }
    }

    /**
     * 更新触发器  只能更新触发 的参数和动作
     *
     * @param map
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateTrigger", method = RequestMethod.POST)
    public EasyResult updateTrigger(@RequestBody Map map, HttpServletRequest request) {
        String userkey = UserKeyUtil.getUserInfoFromToken(request, jedisClient);
        Easytrigger trigger = new Easytrigger();
        /*--------------检查必要的参数------------*/

        //触发器编号
        if (map.containsKey("triggerId")) trigger.setTriggerId(Integer.parseInt(map.get("triggerId").toString()));
        else return EasyResult.fail("缺失触发器编号");

        /*--------------完成其余参数的设定------------*/
        //传感器编号
        if (map.containsKey("sensorId")) trigger.setSensorId(Integer.parseInt(map.get("sensorId").toString()));
        //动作
        if (map.containsKey("actionId")) trigger.setActionId(Integer.parseInt(map.get("actionId").toString()));
        //比较符
        if (map.containsKey("triggerCondition")) {
            int condition = conditionToNumner(map.get("triggerCondition").toString());
            trigger.setTriggerCondition(condition);
        }
        //临界数字
        if (map.containsKey("triggerNumber")) trigger.setTriggerNumber(map.get("triggerNumber").toString());

        //名字
        if (map.containsKey("triggerName")) trigger.setTriggerName(map.get("triggerName").toString());
        try {
            triggerService.updateTrigger(trigger, userkey);
            return EasyResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return EasyResult.fail(e.getMessage());
        }
    }

    /**
     * 获取传感器名下所有的触发器
     *
     * @param sensorId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchSensorAllTrigger/{sensorId}", method = RequestMethod.GET)
    public EasyResult searchSensorAllTrigger(@PathVariable Integer sensorId, HttpServletRequest request) {
        String userKey = UserKeyUtil.getUserInfoFromToken(request, jedisClient);
        try {
            List<Easytrigger> list = triggerService.searchSensorAllTrigger(sensorId, userKey);
            return EasyResult.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return EasyResult.fail(e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getSensorAllTriggers/{sensorId}", method = RequestMethod.GET)
    public EasyResult getSensorAllTriggers(@PathVariable int sensorId, HttpServletRequest request) throws Exception {
        String userKey = UserKeyUtil.getUserInfoFromToken(request, jedisClient);
        List<TriggerInfoVo> list = triggerService.getSensorAllTriggers(sensorId, userKey);
        return EasyResult.ok(list);
    }

    @ResponseBody
    @RequestMapping("/getTriggerInfo/{triggerId}")
    public EasyResult getTriggerInfo(@PathVariable int triggerId, HttpServletRequest request) {
        String userKey = UserKeyUtil.getUserInfoFromToken(request, jedisClient);
        try {
            Easytrigger triggerInfo = triggerService.getTriggerInfo(triggerId, userKey);
            return EasyResult.ok(triggerInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return EasyResult.fail(e.getMessage());
        }
    }

    /**
     * 将string类型的比较符转换成对应的数字
     *
     * @param condition
     * @return
     */
    private int conditionToNumner(String condition) {
        for (TriggerCondtion con : TriggerCondtion.values()) {
            if (con.getName().equals(condition)) return con.getIndex();
        }
        return -1;
    }


}
